#/bin/sh

function modify_conf()
{
    modify_m=$1
    modify_env=$2
    echo modify module $modify_m config
    if [  "$modify_m" != "lebocloud-developer-boot" -a "$modify_m" != "lebocloud-integrated-boot" -a  "$modify_m" != "lebocloud-service" \
        -a "$modify_m" != "sso-cas-boot" -a "$modify_m" != "userauth"  -a "$modify_m" != "statistics-platform" -a "$modify_m" != "lebocloud-common-interface" \
        -a "$modify_m" != "lebocloud-integrated-ad" -a "$modify_m" != "lebocloud-charge-boot" -a "$modify_m" != "play-control"  -a "$modify_m" != "Ad-Service" ];then
        echo have no need to modify
        return
    else
        old_conf=`grep -r "active:" $config_file`
        [ "$modify_env" == "0" ] &&  new_conf="        active: prd"  #正式环境
        [ "$modify_env" == "1" ] &&  new_conf="        active: test" #测试环境
        echo ---------------evn=$modify_env,old_conf=$old_conf and new_conf=$new_conf-------------------
        [ "x$old_conf" == "x" ] &&   echo cannot find active string and exit 1 && exit 1
        sed -i "s/$old_conf/$new_conf/g" $config_file
        #正式环境，统计平台需要修改端口号为8888
        if [ "$modify_env" == "0" -a "$modify_m" == "statistics-platform" ] ;then 
            echo statistics platform release env need use 8888 port 
            old_port=`grep "port:" $config_file`
            new_port="    port: 8888"
            echo old_port=$old_port
            sed -i s/"$old_port"/"$new_port"/g $config_file
            echo modify port result
            grep -rn "port:" $config_file
            new_mport=`grep "port:" $config_file|awk -F ":" '{print $2}'`
            echo new_mport="$new_mport"
            [ "$new_mport" != " 8888" ] && echo modify failed and exit && exit 1
            echo modify success:`grep -rn "port:" $config_file`
        fi
        check_new_conf=`grep -r "$new_conf" $config_file`
        echo ------------------check_new_conf=$check_new_conf------------------------------------
        [ "x$check_new_conf" == "x" ] &&  echo check_new_conf is null and exit && exit 1
        dev_name=`echo $new_conf |awk -F ":" '{print $2}' |awk -F "\"" '{print $1}' | sed 's/ //g'`
        echo dev_name=$dev_name
        #九个需要改动的模块，下面四个的情况不满足test环境的修改:
        #(1)lebocloud-service,userauth,play-control,lebocloud-developer-boot,lebocloud-integrated-ad,Ad-Service  没有application.properties
        #(2)sso-cas-boot  application.properties 没有区分环境
        #(3)lebocloud-common-interface   application.properties 内容是空的
        if [ "$modify_m" == "lebocloud-integrated-boot" -o "$modify_m" == "statistics-platform" -o "$modify_m" == "lebocloud-charge-boot" ];then
            echo module is $modify_m
            if [ "$dev_name" == "test" ];then
                test_line_tmp=`grep -rn "003 test" $properties_file  |awk -F ":" '{print $1}'`
                [ "x$test_line_tmp" ==  "x" ] &&  echo not find 003 test && exit 1 
                test_line_tmp2=`expr $test_line_tmp + 1`
                echo test_line_tmp=$test_line_tmp and test_line_tmp2=$test_line_tmp2 
                sed -n "$test_line_tmp"p $properties_file  > "$properties_file".bak
                sed -n "$test_line_tmp2,\$p" $properties_file |sed s/"#"//g >> "$properties_file".bak #显示文件的匹配行下一行到最后一行内容并把#号删除后写入文件
                mv "$properties_file".bak $properties_file
                echo -------------modify as follow---------------------
                cat $properties_file
            fi
        fi
        echo modify config as follow:
        git diff $config_file
    fi
}


function sdk_auth_build()
{
    if [ "$1" != "sdkAuth" ];then
        echo this is not sdk Auth, do nothing
        return
    fi
    sdkauth_file="src/main/resources/author_prd.properties"
    # 2-> imdns; 3 -> checkToken
    mkdir bak_jar
    mv target/sdkAuth.jar bak_jar/
    echo --------------building sdkAuth imdns------------
    sed -i 's/app_type=1/app_type=2/g' $sdkauth_file
    git diff $sdkauth_file
    mvn clean package
    mv target/sdkAuth.jar bak_jar/imdns.jar
    echo --------------building sdkAuth checkToken------------
    sed -i 's/app_type=2/app_type=3/g' $sdkauth_file
    git diff $sdkauth_file
    mvn clean package
    mv target/sdkAuth.jar bak_jar/checkToken.jar
    mv target/lib bak_jar/
    rm target -rf
    mv bak_jar target
    echo sdkauth  all pkg as follow:
    ls target
}

echo start LBCLOUD branch=$1 and module=$2 and lib_change=$3 and test_env=$4 and released_tag=$5
start_time=`date +%s`
source /etc/profile
lib_flag=$3
test_env=$4
released_tag="null"
[ "x$5" != "x" ] && released_tag=$5
if [ "$released_tag" == "null" ] ;then
    git checkout $1
    git pull
    tmp_dir=`echo $1 |sed 's/\.//g'`
else
    echo released_tag=$released_tag
    git checkout $released_tag
    tmp_dir=$released_tag
fi
now_time=`date "+%Y%m%d%H%M"`
commit=`git rev-parse --short HEAD`
now_time="$now_time"_"$tmp_dir"_"$commit"
config_file="src/main/resources/config/application.yml"
properties_file="src/main/resources/config/application.properties"
wzp_ip="all@120.79.84.79"
wzp_path="/usr/local/package"
#test_env为0是正式环境，为1是测试环境
[ "$test_env" == "0" ] && now_time="$now_time"_release
[ "$test_env" == "1" ] && now_time="$now_time"_test
now_path=`pwd`
lbcloud_repo=`echo $now_path |awk -F "workspace/" '{print $2}'`
echo repo_name=$lbcloud_repo
cloud_ftp=lbcloud/$lbcloud_repo/$now_time 
echo create new folder $cloud_ftp at remote ftp server
curl -u aolingli:ALL1234! ftp://192.168.8.251/output/$cloud_ftp/ --ftp-create-dirs
[ "$test_env" == "0" ] && sshpass -p "Hpplay@321" ssh "$wzp_ip" "mkdir -p  $wzp_path/$lbcloud_repo/$now_time" #打正式环境的包，要传运维路径，测试环境的包不需要传运维路径
input_mode=`echo $2 |sed 's/"//g'`
echo $input_mode > lbcloud_module
if [ "x`echo $input_mode |grep "," `" == "x" ];then
    echo just only input one pro=$input_mode
    [ "x$input_mode" == "x" ] && echo please input module && exit 1
else
    echo input one more pro=$input_mode
fi
sed -i 's/,/\n/g' lbcloud_module
lbcloud_mfile="lbcloud_module"
lbcloud_path=`pwd`
echo build module as follow:
cat $lbcloud_mfile
#乐播云、广告、授权 需要依赖基础库
if [ "$lbcloud_repo" == "LBCloud-AD" -o "$lbcloud_repo" == "LBCloud-Auth" -o "$lbcloud_repo" == "LBCloud-Plat" ];then 
    git clone ssh://gerrit@192.168.8.250:29418/cloud/LBCloud-Common
    if [ -f netty_config ]; then 
        netty_branch=`cat netty_config |grep netty_branch |awk -F "=" '{print $2}'`
        netty_tag=`cat netty_config |grep netty_released_tag |awk -F "=" '{print $2}'`
        cd LBCloud-Common 
        if [ "x$netty_tag" != "x" ]; then
            echo netty_tag=$netty_tag
            git checkout $netty_tag
        else
            echo just use branch:$netty_branch
            git checkout $netty_branch
            git pull 
        fi
    else
        echo have no netty config and use master branch
        cd LBCloud-Common 
        git checkout master
        git pull 
    fi
    echo now_path is netty path=`pwd`
    cd ../ 
    mv LBCloud-Common/NettyHttp ./ 
    [ $? -ne 0 ] && exit 1
    cd NettyHttp
    echo +++++++++++++++++++++++++++++++++++++building NettyHttp++++++++++++++++++++++++++++++++++++
    echo now_path is `pwd`
    if [ -d /root/.m2/repository/NettyHttp ];then
        echo rm old /root/.m2/repository/NettyHttp
        rm -fr /root/.m2/repository/NettyHttp
    fi
    mvn clean install
fi 
cd $lbcloud_path
for lbcloud_mb in `cat $lbcloud_mfile`
    do
    {
        echo ++++++++++++++++++++++++++++++++++building $lbcloud_mb+++++++++++++++++++++++++++++++++++
        cd $lbcloud_mb
        mkdir tmp_target
        if [ "$lbcloud_mb" != "goim" -a "$lbcloud_mb" != "go_proj" ];then
            echo need to modify env  config
            modify_conf $lbcloud_mb $test_env
            mvn clean package
            sdk_auth_build $lbcloud_mb
            echo cp  module jar or war
            [ "`ls target/ |grep ".war$" |wc -l`" != "0" ] && echo have war && cp target/*.war tmp_target/ -rn #判断是否有war包
            [ "`ls target/ |grep ".jar$" |wc -l`" != "0" ] && echo have jar && cp target/*.jar tmp_target/ -rn #判断是否有jar包
            [ "`ls target/ |grep ".war$" |wc -l`" == "0" -a "`ls target/ |grep ".jar$" |wc -l`" == "0" ] && echo have no jar or war && exit 1

            if [ "$lib_flag" == "1" ];then
                echo path=`pwd`
                if [ -d target/lib ];then
                    echo need to upload lib to ftp
                    cp -rn target/lib tmp_target/
                else
                    echo have no lib dir
                fi
            fi
        else
            if [ "$lbcloud_mb" == "goim" ];then
                echo this is goim module
                goim_path=`pwd`
                chmod 777 build/*
                cd build
                sh build_comet.sh
                sh build_job.sh
                sh build_logic.sh
                sh build_router.sh
                echo cur_path=`pwd`
                cd $goim_path
                cp build/*.tar.gz  tmp_target/
            else
                go_path=`pwd`
                echo this is go_proj module
                go_list="src/adcounter src/dmp src/videosync"
                for go_m in $go_list
                    do
                        echo ========$go_m==========
                        cd $go_m
                        sh build.sh
                        cd $go_path
                        cp $go_m/build/*.tar.gz  tmp_target/
                    done
            fi

        fi
        echo uplaod files as follow:
        ls tmp_target
        echo create new folder $cloud_ftp/$lbcloud_mb at remote ftp server
        curl -u aolingli:ALL1234! ftp://192.168.8.251/output/$cloud_ftp/$lbcloud_mb/ --ftp-create-dirs 
        for file in `ls tmp_target`
        do
        {
            if [ -d tmp_target/$file ];then
                echo tmp_target/$file is dir and need compress
                mv tmp_target/$file ./
                zip -q -r tmp_target/$file".zip" "$file"
                file=$file".zip"
            fi
            echo upload_file=$file
            curl -u aolingli:ALL1234! -T tmp_target/"$file"  ftp://192.168.8.251/output/$cloud_ftp/$lbcloud_mb/
            if [ "$test_env" == "0" ]; then #只在出正式环境时才传包到运维服务器上
                if [ "$file" == "lib.zip" ];then
                    echo this is lib.zip and in order to classify module and add module_name to wzp_path
                    sshpass -p "Hpplay@321"  scp  tmp_target/"$file" "$wzp_ip":$wzp_path/$lbcloud_repo/$now_time/"$lbcloud_mb"_"$file"
                else
                    sshpass -p "Hpplay@321"  scp  tmp_target/"$file" "$wzp_ip":$wzp_path/$lbcloud_repo/$now_time/"$file"
                fi
            fi
        }
        done
        cd $lbcloud_path
        echo cur path=$lbcloud_path
    }
    done

echo  > modify_config
for pro_file in `git status |grep "$properties_file" |awk -F "modified:" '{print $2}'`
    do
        conf_file=`echo $pro_file |sed 's/application.properties/application.yml/g'`
        echo $conf_file env as follow: >> modify_config
        grep -r "active:" $conf_file >> modify_config
        echo $pro_file modify as follow: >> modify_config
        cat $pro_file >> modify_config
        echo ==============config file done================= >> modify_config
        echo  >> modify_config
    done
#git diff >> modify_config
echo upload modify_config to ftp server
curl -u aolingli:ALL1234! -T modify_config  ftp://192.168.8.251/output/$cloud_ftp/
end_time=`date +%s`
cost=$(( $end_time - $start_time ))
echo "Done. Total costs $cost seconds."
echo ftp_path="ftp://192.168.8.251/output/$cloud_ftp/"
[ "$test_env" == "0" ] && echo wzp_path=$wzp_path/$lbcloud_repo/"$now_time" #只在出正式环境时才显示运维服务器路径
echo LBCLOUD Build Done.
